/*========================================================================
#   FileName: public.h
#     Author: lwd
#      Email: lwd_xinghuo@126.com
#   HomePage: 
# LastChange: 2013-04-18 09:59:21
#
#
========================================================================*/
#ifndef __PUBLIC_H_
#define __PUBLIC_H_

#include <stdio.h>
#include <string.h>
#include <sys/msg.h>

#define MAX_LINE 80
#define MY_MQ_ID 222

typedef struct{
	long type;
	int rate;
}Message;

static int msgid;

static inline int init_msg()
{
	fprintf(stdout, "init msg-queue ...!\n");
	msgid = msgget(MY_MQ_ID, 0);
	printf("msgid = %d\n\n\n", msgid);
	if(msgid < 0)
	{
		msgid = msgget(MY_MQ_ID, 0666|IPC_CREAT);
		printf("msgid = %d\n\n", msgid);
		if(msgid >= 0)
		{	return 0;	}
		else
		{	return 1;	}
	}
	return 0;
}

static inline int sendMSG(Message obj)
{
	if(msgid >= 0)
	{
		//send message into queue
		int ret = msgsnd(msgid, (struct  msgbuf*)&obj,\
			   	sizeof(Message), 0);
		if(ret != -1)
		{
			return 0;
		}
	}
	
	return 1;
}

static inline int recvMSG(Message *obj)
{
	printf("msgid = %d\n\n", msgid);
	int ret;
	if(msgid >= 0)
	{
		ret = msgrcv(msgid, (struct msgbuf*)obj, \
			   	sizeof(Message), 1, 0);
		printf("recv:msgid = %d rate = %d\n",msgid, obj->rate);
		if(ret == -1)
		{	return 2;}
		else
		{
			return 0;
		}
	}
	return 1;
}

#endif
